﻿using NetCore8Demo.Models.Param;
using NetCore8Demo.Models.PLC;
using NetCore8Demo.Views.Param;

namespace NetCore8Demo.Views.PLC
{
    public partial class EditPlcAddressForm : Form
    {
        public EditPlcAddressForm(Guid plcInfoId) 
        {
            InitializeComponent();
            _plcInfoId = plcInfoId;
        }
        Guid _plcInfoId;
        //避免修改参数时，直接在程序中生效，编辑类里面统一新建一个对象去修改
        List<PlcAddressInfo> _plcAddressInfoList = new();
        private List<PlcAddressInfo> _showList = new();
        int _comIndex;
        private void PLCEditForm_Load(object sender, EventArgs e)
        {
            LoadPlcAddressInfo();
        }
        //如果外部用的是静态对象，那就关闭时生效。
        //如果外部用的不是静态变量，不想重启程序生效，就要在关闭窗口时加载Json文件
        private void EditPLCForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            StaticPlc.InitStaticPlcAddressInfoList();

        }
        #region 页面按钮
        private void newBtn_Click(object sender, EventArgs e)
        {
            List<ParamInfo> paramList = new();
            int tempIndex = 0;
            if (_showList.Count > 0)
            {
                tempIndex = _showList.Max(t => t.Index) + 1;
            }
            paramList.Add(new ParamInfo("序号", tempIndex + ""));
            paramList.Add(new ParamInfo("地址"));

            ParamInfo param = new ParamInfo
            {
                LabelName = "数据类型",
                OldValue = PlcDataType.Short16+"",
                ParamType = ParamType.ComboBox,
                ValueList = Enum.GetNames(typeof(PlcDataType)).ToList(),
            };
            paramList.Add(param);
            paramList.Add(new ParamInfo("名称"));
            paramList.Add(new ParamInfo("长度","50"));

            BasicParamForm basicParamForm = new BasicParamForm(paramList);
            basicParamForm.ShowDialog();
            if (!basicParamForm.SureFlag)
            {
                return;
            }
            PlcAddressInfo plcAddressInfo = new PlcAddressInfo
            {
                Id = Guid.NewGuid()
            };
            plcAddressInfo.PlcInfoId = _plcInfoId;
            int.TryParse(paramList[0].NewValue, out plcAddressInfo.Index);
            plcAddressInfo.Address = paramList[1].NewValue;
            Enum.TryParse(paramList[2].NewValue, out plcAddressInfo.DataType);
            plcAddressInfo.Name = paramList[3].NewValue;
            int.TryParse(paramList[4].NewValue, out plcAddressInfo.Length);
            _plcAddressInfoList.Add(plcAddressInfo);
            SavaPlcAddressInfoList();
        }

        private void delBtn_Click(object sender, EventArgs e)
        {
            if (PlcAddressListView.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择");
                return;
            }
            _comIndex = PlcAddressListView.SelectedIndices[0];
            string id = this.PlcAddressListView.Items[_comIndex].SubItems[0].Text.Trim();

            _plcAddressInfoList.RemoveAll(p => p.Id == Guid.Parse(id));

            SavaPlcAddressInfoList();
        }

        private void PLCAddressListView_DoubleClick(object sender, EventArgs e)
        {
            _comIndex = PlcAddressListView.SelectedIndices[0];
            string id = this.PlcAddressListView.Items[_comIndex].SubItems[0].Text.Trim();
            PlcAddressInfo? oldInfo = _plcAddressInfoList.FirstOrDefault(p => p.Id == Guid.Parse(id));
            if (oldInfo == null) { return; }

            List<ParamInfo> paramList = [new("序号", oldInfo.Index + ""), new("地址", oldInfo.Address)];
            ParamInfo param = new ParamInfo
            {
                LabelName = "数据类型",
                OldValue = oldInfo.DataType + "",
                ParamType = ParamType.ComboBox,
                ValueList = Enum.GetNames(typeof(PlcDataType)).ToList(),
            };
            paramList.Add(param);
            paramList.Add(new ParamInfo("说明", oldInfo.Name));
            paramList.Add(new ParamInfo("长度", oldInfo.Length + ""));
            

            BasicParamForm basicParamForm = new BasicParamForm(paramList);
            basicParamForm.ShowDialog();
            if (!basicParamForm.SureFlag)
            {
                return;
            }

            foreach (var item in _plcAddressInfoList)
            {
                if (item.Id == Guid.Parse(id))
                {
                    int.TryParse(paramList[0].NewValue, out item.Index);
                    item.Address = paramList[1].NewValue;
                    Enum.TryParse(paramList[2].NewValue, out item.DataType);
                    item.Name = paramList[3].NewValue;
                    int.TryParse(paramList[4].NewValue, out item.Length);
                    break;
                }
            }
            SavaPlcAddressInfoList();
        }

        private void ChangeStateBtn_Click(object sender, EventArgs e)
        {
            if (PlcAddressListView.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择");
                return;
            }
            _comIndex = PlcAddressListView.SelectedIndices[0];
            string id = this.PlcAddressListView.Items[_comIndex].SubItems[0].Text.Trim();
            PlcAddressInfo? oldInfo = _plcAddressInfoList.FirstOrDefault(p => p.Id == Guid.Parse(id));
            if (oldInfo == null) { return; }

            List<ParamInfo> paramList = new();
            ParamInfo param = new ParamInfo
            {
                LabelName = "地址状态",
                OldValue = oldInfo.State + "",
                ParamType = ParamType.ComboBox,
                ValueList = Enum.GetNames(typeof(PlcStateType)).ToList(),
            };
            paramList.Add(param);
            paramList.Add(new("默认值", oldInfo.DefaultValue));

            BasicParamForm basicParamForm = new BasicParamForm(paramList);
            basicParamForm.ShowDialog();
            if (!basicParamForm.SureFlag)
            {
                return;
            }

            foreach (var item in _plcAddressInfoList)
            {
                if (item.Id == Guid.Parse(id))
                {
                    Enum.TryParse(paramList[0].NewValue, out item.State);
                    item.DefaultValue = paramList[1].NewValue;
                    break;
                }
            }
            SavaPlcAddressInfoList();
        }


        private void ChangeEncode_Click(object sender, EventArgs e)
        {
            List<ParamInfo> paramList = new();
            string oldType = "";
            foreach (var item in _plcAddressInfoList)
            {
                if (item.DataType == PlcDataType.String字符串)
                {
                    oldType = item.EncodingType + "";
                }
            }


            ParamInfo param = new ParamInfo
            {
                LabelName = "字符编码",
                OldValue = oldType,
                ParamType = ParamType.ComboBox,
                ValueList = Enum.GetNames(typeof(EncodingType)).ToList(),
            };
            paramList.Add(param);

            BasicParamForm basicParamForm = new BasicParamForm(paramList);
            basicParamForm.ShowDialog();
            if (!basicParamForm.SureFlag)
            {
                return;
            }

            foreach (var item in _plcAddressInfoList.Where(item => item.DataType == PlcDataType.String字符串))
            {
                Enum.TryParse(paramList[0].NewValue, out item.EncodingType);
            }
            SavaPlcAddressInfoList();
        }

        #endregion

        #region 方法

        public void SavaPlcAddressInfoList()
        {
            //升序排列
            _plcAddressInfoList = _plcAddressInfoList.OrderBy(s => s.Index).ToList();
            StaticPlc.SavaPlcAddressInfoList(_plcAddressInfoList);

            LoadPlcAddressInfo();
        }

        public void LoadPlcAddressInfo()
        {
            _plcAddressInfoList = StaticPlc.LoadPlcAddressInfoList();
            _showList = _plcAddressInfoList.Where(plcInfo => plcInfo.PlcInfoId == _plcInfoId).ToList();
            this.PlcAddressListView.Items.Clear();
            foreach (var item in _showList)
            {

                ListViewItem lvi = new ListViewItem
                {
                    Text = item.Id + ""
                };
                lvi.SubItems.Add(item.Index + "");
                lvi.SubItems.Add(item.Address + "");
                lvi.SubItems.Add(item.DataType.ToString());
                lvi.SubItems.Add(item.Name);
                lvi.SubItems.Add(item.Length + "");
                lvi.SubItems.Add(item.State.ToString());

                this.PlcAddressListView.Items.Add(lvi);
            }
        }

        #endregion

       
    }
}
